<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm"  label-width="80px">
    <el-form-item label="栏目标题" prop="title">
      <el-input v-model="dataForm.title" placeholder="栏目标题"></el-input>
    </el-form-item>
    <el-form-item label="内容" prop="content">
      <!-- <el-input v-model="dataForm.content" placeholder="内容"></el-input> -->
       <div style="border: 1px solid #ccc">
          <Toolbar
            style="border-bottom: 1px solid #ccc"
            :editor="editor"
            :defaultConfig="toolbarConfig"
            :mode="mode"
          />
      
          <Editor
            style="height: 500px; overflow-y: hidden"
            v-model="dataForm.content"
            :defaultConfig="editorConfig"
            :mode="mode"
             @onCreated="onCreated"
          />
        </div>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
 
import { Editor, Toolbar } from "@wangeditor/editor-for-vue"
  export default {                                                                                                                                   
    components: { Editor, Toolbar },
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          title: '',
          content: ''
        },
        editor: null,
        html: '<p>hello</p>',
        toolbarConfig: { },
        editorConfig: {
          placeholder: '请输入内容...' ,
          MENU_CONF: {
            uploadImage: {
                server: 'http://124.223.70.80:8011/upload',
                fieldName: 'file'
                },
          } 
        },
      mode: 'default', 
        dataRule: {
          title: [
            { required: true, message: '栏目标题不能为空', trigger: 'blur' }
          ],
          content: [
            { required: true, message: '内容不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
       onCreated(editor) {
            this.editor = Object.seal(editor) // 一定要用 Object.seal() ，否则会报错
        },
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/xygk/lead/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.title = data.lead.title
                this.dataForm.content = data.lead.content
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/xygk/lead/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'title': this.dataForm.title,
                'content': this.dataForm.content
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    },
  mounted() {
    // 模拟 ajax 请求，异步渲染编辑器
    setTimeout(() => {
      this.html = "<p>Ajax 异步设置内容 HTML</p>"
    }, 1500);
  },
  beforeDestroy() {
    const editor = this.editor
    if (editor == null) return
    editor.destroy(); // 组件销毁时，及时销毁 editor ，重要！！！
  },
  }
</script>
<style src="@wangeditor/editor/dist/css/style.css"></style>